﻿Imports System.Data
Imports System.Data.Linq
Imports TNCDAL

Public Class GeneralClassBAL
    ' Biến toàn cục lưu thông tin người dùng
    Public Shared _User As tblUser

    Public Shared Function FormatDateStyle() As String
        Return "dd/MM/yyyy"
    End Function

    ''' <summary>
    ''' Kiểm tra connectstring trong config với trong .dbml có trùng khớp không
    ''' Trường hợp nếu quên cấu hình connectstring động trong .dbml
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CheckConnectString() As Boolean
        Dim result As Boolean = True
        Dim connectString_Original As String = GeneralClassDAL.GetConnectString()
        Dim connectString_Test = connectString_Original.Replace(GeneralClassDAL.GetGiaTriThanhPhan(connectString_Original, "Initial Catalog"), "master")
        GeneralClassDAL.ChangeConnectString(connectString_Test)
        Dim dc As New TanNamChinhDataContext
        If dc.Connection.ConnectionString <> GeneralClassDAL.GetConnectString() Then
            result = False
        End If
        'Reset
        GeneralClassDAL.ChangeConnectString(connectString_Original)
        dc = New TanNamChinhDataContext
        Return result
    End Function

    Public Shared Function CheckServiceStatus() As String
        Dim msg As String = ""
        Dim datasource As String
        Dim serviceName As String
        Dim serviceStatus As String
        Try
            datasource = GetGiaTriThanhPhan("Data Source")
            serviceName = "MSSQL$" + datasource.Replace(".\", "")
            serviceStatus = HTLibrary.CollectionHelper.CheckService(serviceName)
            If serviceStatus <> "Running" Then
                msg = String.Format("Serivce {0} đang trong trạng thái {1}. Không thể kết nối đến CSDL.", serviceName, serviceStatus)
            End If
        Catch ex As Exception
            'msg = String.Format("DataSource {0} không tồn tại", datasource)
            msg = ex.Message
        End Try
        Return msg
    End Function

    Public Shared Function GetGiaTriThanhPhan(ByVal pName As String) As String
        'Dim regex As Regex
        'Dim pattern As String = ""
        GeneralClassDAL.GetConnectString()
        Dim KQ As String = GeneralClassDAL.GetGiaTriThanhPhan(GeneralClassDAL.dc.Connection.ConnectionString, pName)
        'Dim strTP As String = ""
        ''Dim dc As TanNamChinhDataContext = New TanNamChinhDataContext()

        ''Dim data() As String = Global.TNCVB.My.MySettings.Default.TanNamChinhConnectionString.Split(";")
        'Dim data() As String = GeneralClassDAL.dc.Connection.ConnectionString.Split(";") 'dc.Connection.ConnectionString.Split(";")
        'For i As Integer = 0 To data.GetUpperBound(0)
        '    If data(i).IndexOf(pName) > -1 Then
        '        strTP = data(i)
        '        Exit For
        '    End If
        'Next
        'Dim data2() As String = strTP.Split("=")
        'If data2.GetUpperBound(0) > 0 Then
        '    KQ = data2(1)
        'End If
        Return KQ
    End Function

    ' '' Retrieve a connection string by specifying the providerName. 
    ' '' Assumes one connection string per provider in the config file. 
    ''Private Shared Function GetConnectionStringByProvider( _
    ''    ByVal providerName As String) As String

    ''    'Return Nothing on failure. 
    ''    Dim returnValue As String = Nothing

    ''    ' Get the collection of connection strings. 
    ''    Dim settings As ConnectionStringSettingsCollection = _
    ''        ConfigurationManager.ConnectionStrings

    ''    ' Walk through the collection and return the first  
    ''    ' connection string matching the providerName. 
    ''    If Not settings Is Nothing Then
    ''        For Each cs As ConnectionStringSettings In settings
    ''            If cs.ProviderName = providerName Then
    ''                returnValue = cs.ConnectionString
    ''                Exit For
    ''            End If
    ''        Next
    ''    End If

    ''    Return returnValue
    ''End Function

    'Private Shared Function EnCrypt(ByVal strEnCrypt As String, ByVal key As String) As String
    '    Try
    '        Dim keyArr As Byte()
    '        Dim EnCryptArr As Byte() = UTF8Encoding.UTF8.GetBytes(strEnCrypt)
    '        Dim MD5Hash As New MD5CryptoServiceProvider()
    '        keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
    '        Dim tripDes As New TripleDESCryptoServiceProvider()
    '        tripDes.Key = keyArr
    '        tripDes.Mode = CipherMode.ECB
    '        tripDes.Padding = PaddingMode.PKCS7
    '        Dim transform As ICryptoTransform = tripDes.CreateEncryptor()
    '        Dim arrResult As Byte() = transform.TransformFinalBlock(EnCryptArr, 0, EnCryptArr.Length)
    '        Return Convert.ToBase64String(arrResult, 0, arrResult.Length)
    '    Catch ex As Exception
    '    End Try
    '    Return ""
    'End Function

    'Private Shared Function DeCrypt(ByVal strDecypt As String, ByVal key As String) As String
    '    Try
    '        Dim keyArr As Byte()
    '        Dim DeCryptArr As Byte() = Convert.FromBase64String(strDecypt)
    '        Dim MD5Hash As New MD5CryptoServiceProvider()
    '        keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
    '        Dim tripDes As New TripleDESCryptoServiceProvider()
    '        tripDes.Key = keyArr
    '        tripDes.Mode = CipherMode.ECB
    '        tripDes.Padding = PaddingMode.PKCS7
    '        Dim transform As ICryptoTransform = tripDes.CreateDecryptor()
    '        Dim arrResult As Byte() = transform.TransformFinalBlock(DeCryptArr, 0, DeCryptArr.Length)
    '        Return UTF8Encoding.UTF8.GetString(arrResult)
    '    Catch ex As Exception
    '    End Try
    '    Return ""
    'End Function
End Class
